package cn.stylefeng.guns.modular.rest;


import cn.stylefeng.guns.modular.wordstudy.entity.Favorite;
import cn.stylefeng.guns.modular.wordstudy.entity.Word;
import cn.stylefeng.guns.modular.wordstudy.model.params.StudyHistoryParam;
import cn.stylefeng.guns.modular.wordstudy.model.result.StudyHistoryResult;
import cn.stylefeng.guns.modular.wordstudy.service.FavoriteService;
import cn.stylefeng.guns.modular.wordstudy.service.StudyHistoryService;
import cn.stylefeng.guns.modular.wordstudy.service.WordService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


/**
 * 绑定控制器
 *
 * @author
 * @Date 2019-11-18 13:24:11
 */
@RestController
@RequestMapping("/gunsApi/favorite")
public class ApiNeedTokenFavoriteController extends BaseController {

    @Autowired
    private FavoriteService favoriteService;
    @Autowired
    private WordService wordService;

    @PostMapping(value = "add")
    public Object add(Favorite favorite) {

        List<Favorite> list = favoriteService.list(new QueryWrapper<Favorite>().eq("type", favorite.getType()).eq("open_id", favorite.getOpenId()).eq("word_id", favorite.getWordId()));
        if (list.size() == 0) {
            favoriteService.save(favorite);
        }
        return SUCCESS_TIP;
    }

    @PostMapping(value = "delete")
    public Object delete(Favorite favorite) {

        favoriteService.remove(new QueryWrapper<Favorite>().eq("open_id", favorite.getOpenId()).eq("word_id", favorite.getWordId()));

        Map map = new HashMap();
        List<Favorite> favorites = favoriteService.list(new QueryWrapper<Favorite>().eq("open_id", favorite.getOpenId()));
        List<Integer> favs = new ArrayList<>();
        List<Integer> errors = new ArrayList<>();
        favorites.forEach(favoriteT -> {
            // 收藏
            if ("1".equals(favoriteT.getType())) {
                favs.add(favoriteT.getWordId());
            } else {
                errors.add(favoriteT.getWordId());
            }
        });
        if (!favs.isEmpty()) {
            map.put("favs", wordService.list(new QueryWrapper<Word>().in("id", favs)));
        }
        if (errors.size() > 0) {
            map.put("errors", wordService.list(new QueryWrapper<Word>().in("id", errors)));
        }
        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }

    @GetMapping(value = "list")
    public Object list(String openId) {
        Map map = new HashMap();
        List<Favorite> favorites = favoriteService.list(new QueryWrapper<Favorite>().eq("open_id", openId));
        List<Integer> favs = new ArrayList<>();
        List<Integer> errors = new ArrayList<>();
        favorites.forEach(favorite -> {
            // 收藏
            if ("1".equals(favorite.getType())) {
                favs.add(favorite.getWordId());
            } else {
                errors.add(favorite.getWordId());
            }
        });
        if (!favs.isEmpty()) {
            map.put("favs", wordService.list(new QueryWrapper<Word>().in("id", favs)));
        }
        if (errors.size() > 0) {
            map.put("errors", wordService.list(new QueryWrapper<Word>().in("id", errors)));
        }
        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }

}


